<template>
  <div class="rich_content">
    <div class="line-t-20"></div>
    <div id="picPrev" class="picPrev nopaddingover">
      <div class="tech-old-img">
        <img :style="{'width':picW}" :src="oldImgUrl" />
      </div>
      <div v-if="drawMove" class="tech-new-img" :style="{'width':offsetX+'px'}">
        <img :style="{'width':picW}" :src="newImgUrl" />
      </div>
      <div v-if="showScan" class="tech-recognition-scan"></div>
      <div v-if="drawMove" id="dragLine" class="tech-drag-container" @touchstart.stop="TouchStart" @touchmove.stop="TouchMove" @touchend.stop="TouchEnd" :style="{'left':(offsetX - 16) +'px'}">
        <div class="tech-drag">
          <div class="tech-drag-line"></div>
          <img ondragstart="return false" src="//aip.bdstatic.com/portal-pc-node/dist/1623836736055/images/technology/common/drag.png" class="tech-drag-icon"></div>
      </div>
    </div>
    <div class="line-t-20"></div>
    <div class="features">
      <van-row gutter="20">
        <van-col span="12">
          <div class="entry e-left" @tap="getImgFromAC('camera')">
            <h5>拍照</h5>
          </div>
        </van-col>
        <van-col span="12">
          <div class="entry e-center" @tap="getImgFromAC('album')">
            <h5>从相册里面选</h5>
          </div>
        </van-col>
      </van-row>
    </div>
    <div class="line-t-10"></div>
    <div class="share">
      <van-row gutter="20">
        <van-col span="24">
          <div class="entry e-left" @tap="showShareSheet">
            <h5>分享</h5>
          </div>
        </van-col>
      </van-row>
    </div>

    <Croper @comicsFusion="comicsFusion" @cancelCropper="cancelCropper" v-if="showCroper" :img-url="imgUrl"></Croper>
    <van-share-sheet
      :show="showShare"
      :options="options"
      @select="shareSelect"
      @close="showShareSheet"
    />
  </div>
</template>

<script>
import PageLoading from "../../components/PageLoading";
import {showToast} from "../../utils";
import Croper from "../../components/Croper";
import {utils} from "../../utils/func";
import {API, getComics} from "../../api";

export default {
  data () {
    return {
      /*拖拽元素父级盒子宽度*/
      containerX:0,
      initX : 0,
      showShare: false,//是否显示分享面板
      options: [{name: '保存至相册', icon: 'poster',}, {name: '分享海报', icon: 'wechat-moments',}],
      oldImgUrl:"https://oss.ido.icu/images/b.png",
      newImgUrl:"https://oss.ido.icu/images/b_n.png",
      /*移动偏移量*/
      offsetX:0,
      picW:"0px",
      dragW:32,
      showCroper:false,
      imgUrl:"",
      /*显示移动*/
      drawMove:true,
      showScan:false
    }
  },

  components: {Croper, PageLoading},

  methods: {
    /*显示/隐藏 分享面板*/
    showShareSheet(){
      this.showShare = !this.showShare;
    },
    shareSelect(o){
      console.log(o);
    },
    shareClose(){
      console.log('shareClose');
    },
    getImgFromAC(type){
      let _this = this;
      utils.getAlbum(type,(res)=>{
        _this.imgUrl = res;
        _this.showCroper = true;
      });
    },
    TouchStart(event){
      let _this = this;
     // _this.offsetX = event.target.offsetLeft;//当前元素相对父盒子的X
      _this.initX = event.touches[0].clientX;
    },
    TouchMove(event){
      let _this = this;
      if(event.mp.changedTouches[0].pageX <= 0){
        _this.offsetX = 0;
      } else if(event.mp.changedTouches[0].pageX >= _this.containerX ){
        _this.offsetX = _this.containerX;
      } else {
        _this.offsetX = event.mp.changedTouches[0].pageX;
      }
    },
    TouchEnd(event){
      console.warn(event);
      // let _this = this;
      // if(event.mp.changedTouches[0].pageX <= 0){
      //   _this.offsetX = 0;
      // } else if(event.mp.changedTouches[0].pageX >= _this.containerX ){
      //   _this.offsetX = _this.containerX;
      // }
     // console.log("event.pageX:",event.mp.changedTouches[0].pageX,"TouchEnd _this.offsetX:",_this.offsetX);
    },
    cancelCropper(){
      this.showCroper = false;
    },
    autoDrawMove(t,x){
      let tmpV = null;
      let _this = this;
      _this.offsetX = 0;
      tmpV = setInterval(()=>{
        _this.offsetX++;
        if(_this.offsetX <= 0){
          _this.offsetX = 0;
          clearInterval(tmpV);
        } else if(_this.offsetX >= x ){
          _this.offsetX = x;
          clearInterval(tmpV);
        } else {
          _this.offsetX = Math.floor(_this.offsetX);
        }
      },t);
    },
    comicsFusion(params) {
      let _this = this;
      _this.drawMove = false;
      _this.showScan = true;
      _this.showCroper = false;
      _this.oldImgUrl = params.local;

      API.getComics({base64:params.base64}).then( res => {
        if(res.code == 200){
          _this.newImgUrl = "data:image/png;base64," + res.data.info;
          _this.showScan = false;
          _this.drawMove = true;
          _this.autoDrawMove(10,_this.containerX);
        } else {
          showToast(res.message);
        }
      })

      setTimeout(()=>{


      },5000);
    }

  },
  mounted() {
    let _this = this;
    //页面中直接调用
    utils.getElement('#picPrev',(res)=>{
      _this.containerX = res[0].width;
      _this.picW = res[0].width + 'px';
      _this.autoDrawMove(20,_this.containerX / 2);
    });
  },

  created () {

  }
}
</script>

<style scoped>
  .picPrev{
    width: 300px;
    height: 300px;
    background-color: #ABCDEF;
    margin: 0 auto;
    position: relative;
    border-radius: 10px;
  }
  .picPrev .tech-old-img,.picPrev .tech-new-img{
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
  }
  .picPrev .tech-old-img img,.picPrev .tech-new-img img{
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
  }
  .picPrev .tech-new-img{
    overflow: hidden;
  }
  .picPrev .tech-drag-container {
    position: absolute;
    left: 0;
    top: 0;
    width: 32px;
    height: 100%;
  }
  .picPrev .tech-drag-container .tech-drag{
    width: 100%;
    cursor: pointer;
  }
  .picPrev .tech-drag-container .tech-drag .tech-drag-line {
    position: absolute;
    left: 15px;
    top: 0;
    width: 2px;
    height: 100%;
    background: #0073eb;
  }
  .picPrev .tech-drag-container .tech-drag .tech-drag-icon {
    position: absolute;
    width: 32px;
    height: 32px;
    margin: auto;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }

  .picPrev .tech-recognition-scan{
    position: absolute;
    z-index: 7;
    top: 0;
    left: 0;
    width: 100%;
    border-bottom: 3px solid #3e88f1;
    -webkit-animation: scan 1.2s infinite;
    -moz-animation: scan 1.2s infinite;
    animation: scan 1.2s infinite;
    background: -webkit-gradient(linear,left top,left bottom,from(transparent),to(#3e88f1));
    background: -webkit-linear-gradient(top,transparent,#3e88f1);
    background: -moz-linear-gradient(top,transparent,#3e88f1);
    background: linear-gradient(
      180deg
      ,transparent,#3e88f1);
  }

  .features,.share {
    width: 300px;
    margin: 0 auto;
  }

  .features .entry.e-left,.share .entry.e-left {
    background-image: linear-gradient(to right top, #ec75b1, #f083ba, #f591c3, #f99fcb, #fdacd4);
  }

  .features .entry.e-center,.share .entry.e-center {
    background-image: linear-gradient(to right top, #4a94e1, #569ce5, #62a4ea, #6dacee, #78b4f2);
  }

  .features .entry ,.share .entry{
    background: orange;
    border-radius: 5px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .25);
    padding: 10px;
    font-size: 13px;
    margin-top: 4px;
    color: #fff;
    text-align: center;
  }



  @keyframes scan{
    0%{
      height:0px;
    }
    to{
      opacity:0;
      height:300px;
    }
  }

  .slide-up-enter-active{
    animation: slide-in .5s;
  }
  .slide-up-leave-active{
    animation: slide-in .5s reverse;
  }
  @keyframes slide-in {
    from {
      -webkit-transform: translate3d(0, 100%, 0);
      transform: translate3d(0, 100%, 0);
      visibility: visible;
    }

    to {
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
  }
  @keyframes slide-in {
    from {
      -webkit-transform: translate3d(0, 100%, 0);
      transform: translate3d(0, 100%, 0);
      visibility: visible;
    }

    to {
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
  }

</style>
